#!/bin/sh

if [ -z $1 ];then
	uname1=$(uname)
	if [ 'Linux' = $uname1 ];then
      day=$(date -d "1 days ago" +%Y%m%d)
    else
	  day=$(date -v -1d +%Y%m%d)
    fi
else
  day=$1
fi

part_day=`date -d "${day}" +%Y%m01`
hive_db=test
sql=$(cat <<EOF
insert overwrite table ${hive_db}.vehicle_hour_info_month select uid, tid, case when vehicle_hour_info.hour0 is null then 0 else 1 end, vehicle_hour_info.hour0.time hour0_time, vehicle_hour_info.hour0.mileage hour0_mileage, vehicle_hour_info.hour0.fuel hour0_fuel, case when vehicle_hour_info.hour1 is null then 0 else 1 end, vehicle_hour_info.hour1.time hour1_time, vehicle_hour_info.hour1.mileage hour1_mileage, vehicle_hour_info.hour1.fuel hour1_fuel, case when vehicle_hour_info.hour2 is null then 0 else 1 end, vehicle_hour_info.hour2.time hour2_time, vehicle_hour_info.hour2.mileage hour2_mileage, vehicle_hour_info.hour2.fuel hour2_fuel, case when vehicle_hour_info.hour3 is null then 0 else 1 end, vehicle_hour_info.hour3.time hour3_time, vehicle_hour_info.hour3.mileage hour3_mileage, vehicle_hour_info.hour3.fuel hour3_fuel, case when vehicle_hour_info.hour4 is null then 0 else 1 end, vehicle_hour_info.hour4.time hour4_time, vehicle_hour_info.hour4.mileage hour4_mileage, vehicle_hour_info.hour4.fuel hour4_fuel, case when vehicle_hour_info.hour5 is null then 0 else 1 end, vehicle_hour_info.hour5.time hour5_time, vehicle_hour_info.hour5.mileage hour5_mileage, vehicle_hour_info.hour5.fuel hour5_fuel, case when vehicle_hour_info.hour6 is null then 0 else 1 end, vehicle_hour_info.hour6.time hour6_time, vehicle_hour_info.hour6.mileage hour6_mileage, vehicle_hour_info.hour6.fuel hour6_fuel, case when vehicle_hour_info.hour7 is null then 0 else 1 end, vehicle_hour_info.hour7.time hour7_time, vehicle_hour_info.hour7.mileage hour7_mileage, vehicle_hour_info.hour7.fuel hour7_fuel, case when vehicle_hour_info.hour8 is null then 0 else 1 end, vehicle_hour_info.hour8.time hour8_time, vehicle_hour_info.hour8.mileage hour8_mileage, vehicle_hour_info.hour8.fuel hour8_fuel, case when vehicle_hour_info.hour9 is null then 0 else 1 end, vehicle_hour_info.hour9.time hour9_time, vehicle_hour_info.hour9.mileage hour9_mileage, vehicle_hour_info.hour9.fuel hour9_fuel, case when vehicle_hour_info.hour10 is null then 0 else 1 end, vehicle_hour_info.hour10.time hour10_time, vehicle_hour_info.hour10.mileage hour10_mileage, vehicle_hour_info.hour10.fuel hour10_fuel, case when vehicle_hour_info.hour11 is null then 0 else 1 end, vehicle_hour_info.hour11.time hour11_time, vehicle_hour_info.hour11.mileage hour11_mileage, vehicle_hour_info.hour11.fuel hour11_fuel, case when vehicle_hour_info.hour12 is null then 0 else 1 end, vehicle_hour_info.hour12.time hour12_time, vehicle_hour_info.hour12.mileage hour12_mileage, vehicle_hour_info.hour12.fuel hour12_fuel, case when vehicle_hour_info.hour13 is null then 0 else 1 end, vehicle_hour_info.hour13.time hour13_time, vehicle_hour_info.hour13.mileage hour13_mileage, vehicle_hour_info.hour13.fuel hour13_fuel, case when vehicle_hour_info.hour14 is null then 0 else 1 end, vehicle_hour_info.hour14.time hour14_time, vehicle_hour_info.hour14.mileage hour14_mileage, vehicle_hour_info.hour14.fuel hour14_fuel, case when vehicle_hour_info.hour15 is null then 0 else 1 end, vehicle_hour_info.hour15.time hour15_time, vehicle_hour_info.hour15.mileage hour15_mileage, vehicle_hour_info.hour15.fuel hour15_fuel, case when vehicle_hour_info.hour16 is null then 0 else 1 end, vehicle_hour_info.hour16.time hour16_time, vehicle_hour_info.hour16.mileage hour16_mileage, vehicle_hour_info.hour16.fuel hour16_fuel, case when vehicle_hour_info.hour17 is null then 0 else 1 end, vehicle_hour_info.hour17.time hour17_time, vehicle_hour_info.hour17.mileage hour17_mileage, vehicle_hour_info.hour17.fuel hour17_fuel, case when vehicle_hour_info.hour18 is null then 0 else 1 end, vehicle_hour_info.hour18.time hour18_time, vehicle_hour_info.hour18.mileage hour18_mileage, vehicle_hour_info.hour18.fuel hour18_fuel, case when vehicle_hour_info.hour19 is null then 0 else 1 end, vehicle_hour_info.hour19.time hour19_time, vehicle_hour_info.hour19.mileage hour19_mileage, vehicle_hour_info.hour19.fuel hour19_fuel, case when vehicle_hour_info.hour20 is null then 0 else 1 end, vehicle_hour_info.hour20.time hour20_time, vehicle_hour_info.hour20.mileage hour20_mileage, vehicle_hour_info.hour20.fuel hour20_fuel, case when vehicle_hour_info.hour21 is null then 0 else 1 end, vehicle_hour_info.hour21.time hour21_time, vehicle_hour_info.hour21.mileage hour21_mileage, vehicle_hour_info.hour21.fuel hour21_fuel, case when vehicle_hour_info.hour22 is null then 0 else 1 end, vehicle_hour_info.hour22.time hour22_time, vehicle_hour_info.hour22.mileage hour22_mileage, vehicle_hour_info.hour22.fuel hour22_fuel, case when vehicle_hour_info.hour23 is null then 0 else 1 end, vehicle_hour_info.hour23.time hour23_time, vehicle_hour_info.hour23.mileage hour23_mileage, vehicle_hour_info.hour23.fuel hour23_fuel,day from vehicle_basic_data_info_month where day=${part_day};
EOF
)

`hive --hiveconf "tez.queue.name=spark" -e "${sql}"`
if [ $? -ne 0 ] ; then
        echo "导出到vehicle_hour_info执行失败..."
    else
        echo "导出到vehicle_hour_info执行成功..."
fi
suffix=`date -d "${day}" +%y`
sqoop export \
-D mapred.job.queue.name=spark \
--connect jdbc:mysql://10.69.197.183:3306/data_platform?useSSL=false \
--username zhsyscvtd \
--password 6066b16e80a7a8c3169dca57a393e6de \
--update-key uid,day \
--update-mode allowinsert \
--table vehicle_hour_info_month_${suffix} \
--fields-terminated-by '\t' \
--hcatalog-database dataplatform \
--hcatalog-table vehicle_hour_info \
--columns "uid,tid,hour0_state,hour0_time,hour0_mileage,hour0_fuel,hour1_state,hour1_time,hour1_mileage,hour1_fuel,hour2_state,hour2_time,hour2_mileage,hour2_fuel,hour3_state,hour3_time,hour3_mileage,hour3_fuel,hour4_state,hour4_time,hour4_mileage,hour4_fuel,hour5_state,hour5_time,hour5_mileage,hour5_fuel,hour6_state,hour6_time,hour6_mileage,hour6_fuel,hour7_state,hour7_time,hour7_mileage,hour7_fuel,hour8_state,hour8_time,hour8_mileage,hour8_fuel,hour9_state,hour9_time,hour9_mileage,hour9_fuel,hour10_state,hour10_time,hour10_mileage,hour10_fuel,hour11_state,hour11_time,hour11_mileage,hour11_fuel,hour12_state,hour12_time,hour12_mileage,hour12_fuel,hour13_state,hour13_time,hour13_mileage,hour13_fuel,hour14_state,hour14_time,hour14_mileage,hour14_fuel,hour15_state,hour15_time,hour15_mileage,hour15_fuel,hour16_state,hour16_time,hour16_mileage,hour16_fuel,hour17_state,hour17_time,hour17_mileage,hour17_fuel,hour18_state,hour18_time,hour18_mileage,hour18_fuel,hour19_state,hour19_time,hour19_mileage,hour19_fuel,hour20_state,hour20_time,hour20_mileage,hour20_fuel,hour21_state,hour21_time,hour21_mileage,hour21_fuel,hour22_state,hour22_time,hour22_mileage,hour22_fuel,hour23_state,hour23_time,hour23_mileage,hour23_fuel,day"


if [ $? -ne 0 ] ; then
        echo "${f}执行失败..."
    else
        echo "${f}执行成功..."
fi

